﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using iSonar.Lib.Utils;

namespace iSonar.iCarus.Gui.Controls {
  public partial class HexViewCtrl : UserControl {
    public HexViewCtrl() {
      InitializeComponent();
    }

    public void setBytes(byte[] buff) {
      listView_Hex.Items.Clear();
      Org.BouncyCastle.Utilities.Encoders.HexEncoder _enc = new Org.BouncyCastle.Utilities.Encoders.HexEncoder();
      for (int i = 0; i < buff.Length; i += 16) {
        int _avail = ((buff.Length - i) < 16) ? (buff.Length - i) : 16;
        byte[] _tmp = new byte[_avail];
        Array.Copy(buff, i, _tmp, 0, _avail);
        string[] _cols = new string[] {
          i.ToString("X08"),
          HexEncoder.Encode(_tmp,0,-1," "),
          new ASCIIEncoding().GetString(_tmp)
        };
        ListViewItem _lvi = new ListViewItem(_cols);
        _lvi.Tag = _tmp;
        listView_Hex.Items.Add(_lvi);
      }
      foreach (ColumnHeader _ch in listView_Hex.Columns) {
        _ch.Width = -2;
      }
    }
  }
}
